package gs.android.videopokercollection;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import com.pad.android.iappad.AdController;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class VideoPokerActivity extends Activity {
	public static final int JACKSORBETTER = 0;
	public static final int DEUCESWILD = 1;
	public static final int JOKERWILD = 2;
	
	private AdController controller;
	private TypedArray images, cards, held, layouts;
	private TextView win, bal, play, collectView;
	private Button collect, gamble, deal;
	private Resources resources;
	private TimerTask timerTask;
	private Timer timer;
	private SharedPreferences sp;
	
	private boolean[] checked, dealed;
	private boolean dealing;
	private int[] prizes, hand;
	private int credits, prize, deckSize, ad, index, gameMode;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.poker);
        
        WindowManager.LayoutParams brightness = getWindow().getAttributes();
		brightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        getWindow().setAttributes(brightness);

		controller = new AdController(this, "509790565");
		
        gameMode = getIntent().getExtras().getInt("gamemode", 0);
        
        resources = getResources();
        layouts = resources.obtainTypedArray(R.array.prizelayouts);
        images = resources.obtainTypedArray(R.array.images);
        cards = resources.obtainTypedArray(R.array.cards);
        held = resources.obtainTypedArray(R.array.held);
        
        TypedArray namesLabels = resources.obtainTypedArray(R.array.prizenames);
        TypedArray prizesLabels = resources.obtainTypedArray(R.array.prizes);
        String[] names;
        switch (gameMode) {
        case JACKSORBETTER:
        	names = resources.getStringArray(R.array.jacksorbetternames);
        	prizes = resources.getIntArray(R.array.jacksorbetterprizes);
        	deckSize = 52;
        break;
        case DEUCESWILD:
        	names = resources.getStringArray(R.array.deuceswildnames);
        	prizes = resources.getIntArray(R.array.deuceswildprizes);
        	deckSize = 52;
        break;
        case JOKERWILD:
        	names = resources.getStringArray(R.array.jokerwildnames);	
        	prizes = resources.getIntArray(R.array.jokerwildprizes);
        	deckSize = 53;
        break;
        default:
        	names = new String[0];
        	prizes = new int[0];
        break;
        }
        
        for (int i = 0; i < namesLabels.length(); i++) {
        	if (i < prizes.length) {
	        	TextView name = (TextView) findViewById(namesLabels.peekValue(i).resourceId);
	        	TextView prize = (TextView) findViewById(prizesLabels.peekValue(i).resourceId);
	        	name.setText(names[i]);
	        	prize.setText("" + prizes[i]);
        	} else {
        		RelativeLayout l = (RelativeLayout) findViewById(layouts.peekValue(i).resourceId);
        		l.setVisibility(View.GONE);
        	}
        }
        
        sp = getSharedPreferences("poker", MODE_PRIVATE);
        credits = sp.getInt("credits", 0);
        ad = 0;
        win = (TextView) findViewById(R.id.lblWin);
        win.setText("0");
        bal = (TextView) findViewById(R.id.lblCredits);
        bal.setText("" + credits);
        play = (TextView) findViewById(R.id.lblDoubleWinnings);
        collectView = (TextView) findViewById(R.id.lblCollectWinnings);
        
        dealed = new boolean[deckSize];
        for (int i = 0; i < deckSize; i++)
        	dealed[i] = false;
        
        checked = new boolean[5];
        hand = new int[5];
        for (int i = 0; i < 5; i++) {
        	checked[i] = false;
        	hand[i] = 1 + i;
        	ImageView img = (ImageView) findViewById(cards.peekValue(i).resourceId);
        	img.setTag(i);
        	img.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (deal.getText().equals(resources.getString(R.string.draw))) {
						ImageView img = (ImageView) v;
						int index = (Integer) v.getTag();
						checked[index] = !checked[index];
						if (checked[index])
							img.setImageResource(held.peekValue(hand[index] - 1).resourceId);
						else
							img.setImageResource(images.peekValue(hand[index] - 1).resourceId);
					}
				}
			});
        }

        deal = (Button)findViewById(R.id.btnDeal);
        deal.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				removePrizeHighligh();
				if (deal.getText().equals(resources.getString(R.string.deal)))
					deal();
				else
					draw();
			}
		});
        
        gamble = (Button)findViewById(R.id.btnDouble);
        gamble.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(), GambleActivity.class);
				intent.putExtra("prize", prize);
				startActivityForResult(intent, 1);
			}
		});
        
        collect = (Button) findViewById(R.id.btnCollect);
        collect.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				credits += prize;
				bal.setText("" + credits);
				deal.setVisibility(View.VISIBLE);
				gamble.setVisibility(View.GONE);
				collect.setVisibility(View.GONE);
				play.setText(resources.getString(R.string.dealtoplay));
				collectView.setVisibility(View.INVISIBLE);
			}
		});
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode == RESULT_OK && requestCode == 1) {
    		prize = data.getIntExtra("gamblePrize", 0);
    		credits += prize;
    		bal.setText("" + credits);
    		win.setText("" + prize);
    		deal.setVisibility(View.VISIBLE);
			gamble.setVisibility(View.GONE);
			collect.setVisibility(View.GONE);
			play.setText(resources.getString(R.string.dealtoplay));
			collectView.setVisibility(View.INVISIBLE);
			if (credits == 0){
				newGameMessage();
			}
    	}
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstance) {
    	savedInstance.putBooleanArray("checked", checked);
    	savedInstance.putBooleanArray("dealed", dealed);
    	savedInstance.putIntArray("hand", hand);
    	savedInstance.putString("win", win.getText().toString());
    	savedInstance.putInt("credits", credits);
    	savedInstance.putInt("ad", ad);
    	boolean dealHand = false;
    	if (deal.getText().equals(resources.getString(R.string.deal)))
    		dealHand = true;
    	savedInstance.putBoolean("dealHand", dealHand);
    	super.onSaveInstanceState(savedInstance);
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstance) {
    	checked = savedInstance.getBooleanArray("checked");
    	dealed = savedInstance.getBooleanArray("dealed");
    	hand = savedInstance.getIntArray("hand");
    	boolean dealHand = savedInstance.getBoolean("dealHand");
    	credits = savedInstance.getInt("credits");
    	ad = savedInstance.getInt("ad");
    	bal.setText("" + credits);
    	win.setText(savedInstance.getString("win"));
    	for (int i = 0; i < 5; i++) {
			ImageView img = (ImageView)findViewById(cards.peekValue(i).resourceId);
    		if (checked[i])
    			img.setImageResource(held.peekValue(hand[i]-1).resourceId);
    		else
    			img.setImageResource(images.peekValue(hand[i]-1).resourceId);
    	}
    	if (dealHand)
    		deal.setText(resources.getString(R.string.deal));
    	else
    		deal.setText(resources.getString(R.string.draw));
    	super.onRestoreInstanceState(savedInstance);
    }
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		controller.destroyAd();
	}
    
    private void removeCards() {
    	runOnUiThread(new Runnable() {
			@Override
			public void run() {
				boolean found = false;
				while (!found && index < 5) {
					if (!checked[index]) {
						found = true;
					} else {
						index++;
					}
				}
				if (index < 5) {
					ImageView img = (ImageView) findViewById(cards.peekValue(index).resourceId);
					img.setImageResource(images.peekValue(53).resourceId);
					index++;
				} else {
					timerTask.cancel();
					timer.cancel();
					index = 0;
					timer = new Timer("cards");
			        timerTask = new TimerTask() {
						@Override
						public void run() {
							addCards();
						}
					};
			        timer.scheduleAtFixedRate(timerTask, 0, 100);
				}
			}
    	});
    }
    
    private void addCards() {
    	runOnUiThread(new Runnable() {
			@Override
			public void run() {
				boolean found = false;
				while (!found && index < 5) {
					if (!checked[index]) {
						found = true;
					} else {
						index++;
					}
				}
				if (index < 5) {
					ImageView img = (ImageView) findViewById(cards.peekValue(index).resourceId);
					img.setImageResource(images.peekValue(hand[index] - 1).resourceId);
				} else {
					timerTask.cancel();
					timer.cancel();
			    	deal.setEnabled(true);
					if (dealing) {
						autoCheck();
					} else {
						prize = prize();
						win.setText("" + prize);
						play.setText(resources.getString(R.string.dealtoplay));
						if (prize > 0) {
							deal.setVisibility(View.GONE);
							gamble.setVisibility(View.VISIBLE);
							collect.setVisibility(View.VISIBLE);
							play.setText(resources.getString(R.string.doublewinnings));
							collectView.setVisibility(View.VISIBLE);
						} else if (credits == 0){
								newGameMessage();
						}
						if (ad == 15) {
							ad = 0;
							controller.loadAd();
						}
					}
				}
				index++;
			}
    	});
    }
    
    private void deal(){
    	if (credits >= 5) {
    		dealing = true;
        	deal.setEnabled(false);
    		for (int i = 0; i < 5; i++)
    			checked[i] = false;
    		ad++;
    		credits -= 5;
	    	bal.setText("" + credits);
	    	for (int i = 0; i < deckSize; i++)
				dealed[i] = false;
			deal.setText(R.string.draw);
			play.setText("");
			dealCards();

    		index = 0;
			timer = new Timer("cards");
	        timerTask = new TimerTask() {
				@Override
				public void run() {
					removeCards();
				}
			};
	        timer.scheduleAtFixedRate(timerTask, 0, 100);
    	}
    }
    
    private void draw(){
    	dealing = false;
    	deal.setEnabled(false);
    	for (int i = 0; i < 5; i++) {
			ImageView img = (ImageView) findViewById(cards.peekValue(i).resourceId);
			img.setImageResource(images.peekValue(hand[i] - 1).resourceId);
		}
    	deal.setText(R.string.deal);
		dealCards();

    	index = 0;
		timer = new Timer("cards");
        timerTask = new TimerTask() {
			@Override
			public void run() {
				removeCards();
			}
		};
        timer.scheduleAtFixedRate(timerTask, 0, 100);
    }
    
    private void dealCards() {
    	for (int i = 0; i < 5; i++) { 
    		if (!checked[i]) {
	    		int n = (int)(Math.random() * deckSize) + 1;
	    		while (dealed[n - 1])
	    			n = (int)(Math.random() * deckSize) + 1;
	    		dealed[n - 1] = true;
	    		hand[i] = n;
    		}
    	}
    }
    
    private int prize() {
    	boolean flush = checkFlush();
    	boolean highStraight = checkHighStraight();
    	boolean straight = checkStraight();
    	boolean fiveOfAKind = false;
    	boolean fourOfAKind = false;
    	int[] ofAKind = ofAKind();
    	int threeOfAKind = 0;
    	int pairs = 0;
    	int pair = 0;
    	
    	for (int i = 0; i < 13; i++) {
    		if (ofAKind[i] == 5) {
    			fiveOfAKind = true;
    		} else if (ofAKind[i] == 4) {
    			fourOfAKind = true;;
    		} else if (ofAKind[i] == 3) {
    			threeOfAKind++;
    		} else if (ofAKind[i] == 2) {
    			pairs++;
    			pair = i;
    		}
    	}
    	
    	if (flush && highStraight && isNatural()) {
    		return royalFlush();
    	} else if (fiveOfAKind) {
    		return fiveOfAKind();
    	} else if (flush && highStraight) {
    		return royalFlush();
    	} else if (flush && straight) {
    		return straightFlush();
    	} else if (fourOfAKind) {
    		return fourOfAKind();
    	} else if (threeOfAKind == 1 && pairs == 1 || threeOfAKind == 2) {
    		return fullHouse();
    	} else if (flush) {
    		return flush();
    	} else if (highStraight || straight) {
    		return straight();
    	} else if (threeOfAKind == 1 || threeOfAKind == 3) {
    		return threeOfAKind();
    	} else if (pairs == 2) {
    		return twoPair();
    	} else if (pairs >= 1) {
    		if (gameMode == JACKSORBETTER) {
    			if (pair == 0 || pair == 1 || pair > 10)
        			return pair();
    		} else if (gameMode == JOKERWILD) {
    			if (ofAKind[0] == 2 || ofAKind[1] == 2)
    				return pair();
    		} else
    			return 0;
    	}
    	return 0;
    }
    
    private boolean checkFlush() {
    	boolean res = false;
    	int[] suit = new int[4];
    	for (int i = 0; i < 5; i++) {
			int n = (int)((hand[i] - 1) / 13);
    		switch (gameMode) {
    		case JACKSORBETTER:
        		suit[n]++;
    		break;
    		case DEUCESWILD:
    			if (hand[i] % 13 == 2)
    				for (int j = 0; j < 4; j++)
    					suit[j]++;
    			else
    				suit[n]++;
    		break;
    		case JOKERWILD:
    			if (hand[i] == 53)
    				for (int j = 0; j < 4; j++)
    					suit[j]++;
    			else
    				suit[n]++;
    		break;
    		}
    	}
    	for (int i = 0; i < 4; i++)
    		if (suit[i] == 5)
    			res = true;
    	return res;
    }
    
    private boolean checkHighStraight() {
    	boolean res = false;
		int natural = 0;
    	int[] ofAKind = ofAKind();
    	if (isNatural()) {
    		if (ofAKind[0] == 1 && ofAKind[1] == 1 && ofAKind[10] == 1 && ofAKind[11] == 1 && ofAKind[12] == 1)
    			return true;
    	} else {
    		switch (gameMode) {
    		case DEUCESWILD:
    			int deuces = 0;
    			for (int i = 0; i < 5; i++)
    				if (hand[i] % 13 == 2)
    					deuces++;
        		if (ofAKind[0] >= deuces + 1)
        			natural++;
        		if (ofAKind[1] >= deuces + 1)
        			natural++;
        		for (int i = 10; i < 13; i++)
        			if (ofAKind[i] >= deuces + 1)
        				natural++;
        		if (natural >= 5 - deuces)
        			res = true;
    		break;
    		case JOKERWILD:
    			if (ofAKind[0] == 2)
        			natural++;
        		if (ofAKind[1] == 2)
        			natural++;
        		for (int i = 10; i < 13; i++)
        			if (ofAKind[i] == 2)
        				natural++;
        		if (natural == 4)
        			res = true;
    		break;
    		}
    	}
    	return res;
    }
    
    private boolean checkStraight() {
    	boolean res = false;
    	int[] ofAKind = ofAKind();
    	if (isNatural()) {
	    	for (int i = 1; i < 9; i++) {
	    		if (ofAKind[i] == 1 && ofAKind[i + 1] == 1 && ofAKind[i + 2] == 1 && ofAKind[i + 3] == 1 && ofAKind[i + 4] == 1)
	        		res = true;
	    	}
        	if (!res)
        		if (ofAKind[0] == 1 && ofAKind[9] == 1 && ofAKind[10] == 1 && ofAKind[11] == 1 && ofAKind[12] == 1)
        			res = true;
    	} else {
    		switch (gameMode) {
    		case DEUCESWILD:
    			int deuces = 0;
    			for (int i = 0; i < 5; i++)
    				if (hand[i] % 13 == 2)
    					deuces++;
    			for (int i = 1; i < 9; i++) {
        			int natural = 0;
        			for (int j = 0; j < 5; j++)
        				if (ofAKind[i + j] >= deuces + 1)
        					natural++;
        			if (natural >= 5 - deuces)
        				res = true;
        		}
        		if (!res) {
        			int natural = 0;
        			if (ofAKind[0] >= deuces + 1)
        				natural++;
        			for (int i = 9; i < 13; i++)
        				if (ofAKind[i] >= deuces + 1)
        					natural++;
        			if (natural >= 5 - deuces)
        				res = true;
        		}
    		break;
    		case JOKERWILD:
    			for (int i = 1; i < 9; i++) {
        			int natural = 0;
        			for (int j = 0; j < 5; j++)
        				if (ofAKind[i + j] == 2)
        					natural++;
        			if (natural == 4)
        				res = true;
        		}
    			if (!res) {
        			int natural = 0;
        			if (ofAKind[0] == 2)
        				natural++;
        			for (int i = 9; i < 13; i++)
        				if (ofAKind[i] == 2)
        					natural++;
        			if (natural == 4)
        				res = true;
        		}
    		break;
    		}
    	}
    	return res;
    }
    
    private int royalFlush() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 4000;
			highligtPrize(0);
		break;
    	case DEUCESWILD:
    		if (isNatural()) {
    			prize = 4000;
    			highligtPrize(0);
    		} else {
    			prize = 100;
    			highligtPrize(2);
    		}
    	break;
    	case JOKERWILD:
    		if (isNatural()) {
    			prize = 4000;
    			highligtPrize(0);
    		} else {
    			prize = 500;
    			highligtPrize(2);
    		}
    	break;
    	}
    	return prize;
    }
    
    private int straightFlush() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 250;
			highligtPrize(1);
		break;
    	case DEUCESWILD:
    		prize = 45;
    		highligtPrize(4);
    	break;
    	case JOKERWILD:
    		prize = 250;
    		highligtPrize(3);
    	break;
    	}
    	return prize;
    }
    
    private int fiveOfAKind() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 0;
    	break;
    	case DEUCESWILD:
    		int deuces = 0;
    		for (int i = 0; i < 5; i++)
    			if (hand[i] % 13 == 2)
    				deuces++;
    		if (deuces == 4) {
    			prize = 1000;
    			highligtPrize(1);
    		} else {
	    		prize = 75;
	    		highligtPrize(3);
    		}
    	break;
    	case JOKERWILD:
    		prize = 1000;
    		highligtPrize(1);
    	break;
    	}
    	return prize;
    }
    
    private int fourOfAKind() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 125;
			highligtPrize(2);
		break;
    	case DEUCESWILD:
    		prize = 20;
    		highligtPrize(5);
    	break;
    	case JOKERWILD:
    		prize = 75;
    		highligtPrize(4);
    	break;
    	}
    	return prize;
    }
    
    private int fullHouse() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 45;
			highligtPrize(3);
		break;
    	case DEUCESWILD:
    		prize = 20;
    		highligtPrize(6);
    	break;
    	case JOKERWILD:
    		prize = 40;
    		highligtPrize(5);
    	break;
    	}
    	return prize;
    }
    
    private int flush() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 30;
			highligtPrize(4);
		break;
    	case DEUCESWILD:
    		prize = 15;
    		highligtPrize(7);
    	break;
    	case JOKERWILD:
    		prize = 25;
    		highligtPrize(6);
    	break;
    	}
    	return prize;
    }
    
    private int straight() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 20;
			highligtPrize(5);
		break;
    	case DEUCESWILD:
    		prize = 10;
    		highligtPrize(8);
    	break;
    	case JOKERWILD:
    		prize = 15;
    		highligtPrize(7);
    	break;
    	}
    	return prize;
    }
    
    private int threeOfAKind() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 15;
			highligtPrize(6);
		break;
    	case DEUCESWILD:
    		prize = 5;
    		highligtPrize(9);
    	break;
    	case JOKERWILD:
    		prize = 10;
    		highligtPrize(8);
    	break;
    	}
    	return prize;
    }
    
    private int twoPair() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 10;
			highligtPrize(7);
		break;
    	case DEUCESWILD:
    		prize = 0;
    	break;
    	case JOKERWILD:
    		prize = 5;
    		highligtPrize(9);
    	break;
    	}
    	return prize;
    }
    
    private int pair() {
    	int prize = 0;
    	switch (gameMode) {
    	case JACKSORBETTER:
    		prize = 5;
			highligtPrize(8);
		break;
    	case DEUCESWILD:
    		prize = 0;
    	break;
    	case JOKERWILD:
    		prize = 5;
    		highligtPrize(10);
    	break;
    	}
    	return prize;
    }
    
    private void autoCheck() {
    	switch (gameMode) {
    	case JACKSORBETTER: 	autoCheckJacksOrBetter();	break;
    	case DEUCESWILD: 		autoCheckDeucesWild();		break;
    	case JOKERWILD: 		autoCheckJokersWild();		break;
    	}
    }
    
    private void autoCheckJacksOrBetter() {
    	int autoPrize = prize();
    	switch (autoPrize) {
    	case 4000:	checkAll();			break;
    	case 250:	checkAll();			break;
    	case 125:	checkOfAKind(4);	break;
    	case 45:	checkAll();			break;
    	case 30:	checkAll();			break;
    	case 20:	checkAll();			break;
    	case 15:	checkOfAKind(3);	break;
    	case 10:	checkTwoPair();		break;
    	case 5:		checkOfAKind(2);	break;
    	}
    }
    
    private void autoCheckDeucesWild() {
    	int autoPrize = prize();
    	switch (autoPrize) {
		case 4000:	checkAll();			break;
		case 1000:	checkOfAKind(4);	break;
		case 100:	checkAll();			break;
		case 75:	checkAll();			break;
		case 45:	checkAll();			break;
		case 20:	
			boolean four = false;
			int[] ofAKind = ofAKind();
			for (int i = 0; i < 13; i++)
				if (ofAKind[i] == 4)
					four = true;
			if (four)
				checkOfAKind(4);
			else
				checkAll();				break;
		case 15:	checkAll();			break;
		case 10:	checkAll();			break;
		case 5:		checkOfAKind(3);	break;
    	}
    	for (int i = 0; i < 5; i++) {
    		if (hand[i] % 13 == 2)
    		{
	    		checked[i] = true;
				ImageView img = (ImageView) findViewById(cards.peekValue(i).resourceId);
	    		img.setImageResource(held.peekValue(hand[i] - 1).resourceId);
    		}
    	}
    }
    
    private void autoCheckJokersWild() {
    	int autoPrize = prize();
    	switch (autoPrize) {
		case 4000:	checkAll();			break;
		case 1000:	checkAll();			break;
		case 500:	checkAll();			break;
		case 250:	checkAll();			break;
		case 75:	checkOfAKind(4);	break;
		case 40:	checkAll();			break;
		case 25:	checkAll();			break;
		case 15:	checkAll();			break;
		case 10:	checkOfAKind(3);	break;
		case 5:
			int pairs = 0;
			int[] ofAKind = ofAKind();
			for (int i = 0; i < 13; i++)
	    		if (ofAKind[i] == 2)
	    			pairs++;
			if (pairs == 2)
				checkTwoPair();
			else
				checkOfAKind(2);
		break;
    	}
    	for (int i = 0; i < 5; i++) {
    		if (hand[i] == 53)
    		{
	    		checked[i] = true;
				ImageView img = (ImageView) findViewById(cards.peekValue(i).resourceId);
	    		img.setImageResource(held.peekValue(hand[i] - 1).resourceId);
    		}
    	}
    }
    
    private void checkAll() {
    	for (int i = 0; i < 5; i++) {
    		checked[i] = true;
    		ImageView img = (ImageView) findViewById(cards.peekValue(i).resourceId);
    		img.setImageResource(held.peekValue(hand[i] - 1).resourceId);
    	}
    }
    
    private void checkTwoPair() {
    	int[] ofAKind = ofAKind();
    	for (int i = 0; i < 13; i++) {
			if (ofAKind[i] == 2) {
				for (int j = 0; j < 5; j++) {
					if (hand[j] % 13 == i) {
						checked[j] = true;
						ImageView img = (ImageView) findViewById(cards.peekValue(j).resourceId);
			    		img.setImageResource(held.peekValue(hand[j] - 1).resourceId);
					}
				}
			}
		}
    }
    
    private void checkOfAKind(int n) {
    	int[] ofAKind = ofAKind();
    	boolean found = false;
    	int i = 1;
    	
    	if (ofAKind[i] == n)
    		found = true;
    	else {
    		i = 0;
    		if (ofAKind[0] == n)
    			found = true;
    		else
    			i = 12;
    	}

    	while (!found && i > 1) {
			if (ofAKind[i] == n) {
				found = true;
			} else
				i--;
    	}
    	if (found) {
			for (int j = 0; j < 5; j++) {
				if (hand[j] % 13 == i) {
					checked[j] = true;
					ImageView img = (ImageView) findViewById(cards.peekValue(j).resourceId);
		    		img.setImageResource(held.peekValue(hand[j] - 1).resourceId);
				}
			}
    	}
    }
    
    private int[] ofAKind() {
    	int[] ofAKind = new int[13];
    	for (int i = 0; i < 5; i++) {
    		switch (gameMode) {
    		case JACKSORBETTER:
    			ofAKind[hand[i] % 13]++;
    		break;
    		case DEUCESWILD:	
    			if (hand[i] % 13 == 2)
    				for (int j = 0; j < 13; j++)
    					ofAKind[j]++;
    			else
    	    		ofAKind[hand[i] % 13]++;
    		break;
    		case JOKERWILD:
    			if (hand[i] == 53)
    				for (int j = 0; j < 13; j++)
    					ofAKind[j]++;
    			else
    	    		ofAKind[hand[i] % 13]++;
    		break;
    		}
    	}
    	return ofAKind;
    }
    
    private boolean isNatural() {
    	boolean natural = true;
    	for (int i = 0; i < 5; i++) {
    		switch (gameMode) {
    		case JACKSORBETTER:				break;
    		case DEUCESWILD:
    			if (hand[i] % 13 == 2)
    				natural = false;		break;
    		case JOKERWILD:
    			if (hand[i] == 53)
    				natural = false;		break;
    		}
    	}
    	return natural;
    }
    
    private void highligtPrize(int n) {
    	RelativeLayout l = (RelativeLayout) findViewById(layouts.peekValue(n).resourceId);
    	l.setBackgroundResource(R.drawable.prize_background_light);
    }
    
    private void removePrizeHighligh() {
    	for (int i = 0; i < layouts.length(); i++) {
    		RelativeLayout l = (RelativeLayout) findViewById(layouts.peekValue(i).resourceId);
    		l.setBackgroundResource(R.drawable.prize_background_dark);
    	}
    }
    
    private void startNewGame() {
    	credits = 100;
    	prize = 0;
    	bal.setText("" + credits);
    	win.setText("" + prize);
    }
    
    private void newGameMessage() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(getResources().getString(R.string.newgame))
    	       .setCancelable(false)
    	       .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   startNewGame();
    	        	   dialog.cancel();
    	           }
    	       })
    	       .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   Editor editor = sp.edit();
    	        	   editor.putInt("credits", credits);
    	        	   editor.commit();
    	        	   Intent intent = new Intent(getBaseContext(), MainActivity.class);
    	        	   startActivity(intent);
    	        	   VideoPokerActivity.this.finish();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    private void exitGameMessage() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(getResources().getString(R.string.exitgame))
    	       .setCancelable(false)
    	       .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   Editor editor = sp.edit();
    	        	   editor.putInt("credits", credits);
    	        	   editor.commit();
    	        	   try {
    	        		   for (File f : getCacheDir().getParentFile().listFiles())
    	        				if (f.getName().equals("databases"))
    	        					clearCache(f);
    	        			clearCache(getCacheDir());
    	        	   } catch (Exception e) {
    	        		   
    	        	   }
    	        	   VideoPokerActivity.this.finish();
    	           }
    	       })
    	       .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    private void clearCache(File f) {
    	try {
			for (File child : f.listFiles()) {
				if (child.isDirectory()) {
					clearCache(child);
				} else {
					child.delete();
				}
			}
    	} catch (Exception e) {
    		
    	}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitGameMessage();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}